﻿using System;
using System.Windows;
using Kiva7.Controls;
using Kiva7.DataClasses;
using Kiva7.Images;
using Kiva7.Translations;

namespace Kiva7.Pages
{
    public partial class SearchFilterPage
    {
        private const string Url = "/Pages/SearchFilterPage.xaml";
        public static readonly Uri PageUrl = new Uri(Url, UriKind.RelativeOrAbsolute);

        public SearchFilterPage()
        {
            InitializeComponent();
            CreateStandardControls();
        }

        private void ButtonClick(object sender,
                                 EventArgs e)
        {
            DoNavigate(
                SearchResultPage.GetPageUrl(null,
                                            lpStatus.SelectedItem != null
                                                ? ((StringListPickerItem) lpStatus.SelectedItem).Value
                                                : null,
                                            GetGender(),
                                            ((StringListPickerItem)lpRegion.SelectedItem).Value != ""
                                                ? ((StringListPickerItem) lpRegion.SelectedItem).Value
                                                : null,
                                            null,
                                            (string) lpSector.SelectedItem != "(None)"
                                                ? (string) lpSector.SelectedItem
                                                : null,
                                            null,
                                            ((StringListPickerItem) lpSort.SelectedItem).Value,
                                            null));
        }

        private Gender? GetGender()
        {
            if (rbtFemale.IsChecked.Value)
            {
                return Gender.Female;
            }
            if (rbtMale.IsChecked.Value)
            {
                return Gender.Male;
            }
            return null;
        }

        private void PhoneApplicationPageLoaded(object sender,
                                                RoutedEventArgs e)
        {
            if (lpStatus.Items.Count == 0)
            {
                lpStatus.Items.Add(new StringListPickerItem(AppResources.Fundraising, "fundraising"));
                lpStatus.Items.Add(new StringListPickerItem(AppResources.Funded, "funded"));
                lpStatus.Items.Add(new StringListPickerItem(AppResources.InRepayment, "in_repayment"));
                lpStatus.Items.Add(new StringListPickerItem(AppResources.Paid, "paid"));
                lpStatus.Items.Add(new StringListPickerItem(AppResources.Defaulted, "defaulted"));
                lpStatus.Items.Add(new StringListPickerItem(AppResources.Refunded, "refunded"));
            }

            if (lpRegion.Items.Count == 0)
            {
                lpRegion.Items.Add(new StringListPickerItem(AppResources.None, ""));
                lpRegion.Items.Add(new StringListPickerItem(AppResources.NorthAmerica, "na"));
                lpRegion.Items.Add(new StringListPickerItem(AppResources.CentralAmerica, "ca"));
                lpRegion.Items.Add(new StringListPickerItem(AppResources.SouthAmerica, "sa"));
                lpRegion.Items.Add(new StringListPickerItem(AppResources.Africa, "af"));
                lpRegion.Items.Add(new StringListPickerItem(AppResources.SouthEastAsia, "se"));
                lpRegion.Items.Add(new StringListPickerItem(AppResources.MiddleEast, "me"));
            }

            if (lpSort.Items.Count == 0)
            {
                lpSort.Items.Add(new StringListPickerItem(AppResources.Popularity, "popularity"));
                lpSort.Items.Add(new StringListPickerItem(AppResources.LoanAmount, "loan_amount"));
                lpSort.Items.Add(new StringListPickerItem(AppResources.Oldest, "oldest"));
                lpSort.Items.Add(new StringListPickerItem(AppResources.Expiration, "expiration"));
                lpSort.Items.Add(new StringListPickerItem(AppResources.Newest, "newest"));
                lpSort.Items.Add(new StringListPickerItem(AppResources.AmountRemaining, "amount_remaining"));
                lpSort.Items.Add(new StringListPickerItem(AppResources.RepaymentTerm, "repayment_term"));
            }
        }

        protected override void CreateApplicationBar()
        {
            AddBarButton(AppResources.Search, ImageUrls.Search, ButtonClick);
        }
    }
}